import { WidgetBaseSession } from './base'
import { fromAny, toAny, Int32Value } from "../../proto";
import { Message } from 'protobufjs'
export class WidgetProgressBarSession extends WidgetBaseSession {
    private meter: HTMLMeterElement
    constructor(render: HTMLElement) {
        super('ProgressBar', render)
    }
    createWidget(...argv): HTMLElement {
        return this.meter = document.createElement('meter')
    }
    onReceive(name: String, sessionId: number, futureId: number, ...argv) {
        switch (name) {
            case 'getRange':
                return this.getRange(sessionId, futureId)
            case 'setRange':
                return this.setRange(...argv)
            case 'getValue':
                return this.getValue(sessionId, futureId)
            case 'setValue':
                return this.setValue(...argv)
        }
        super.onReceive(name, sessionId, futureId, ...argv);
    }
    private getRange(sessionId: number, futureId: number) {
        const min = toAny(this.meter.min, Int32Value)
        const max = toAny(this.meter.max, Int32Value)
        this.onGetRange(sessionId, futureId, min, max)
    }
    private onGetRange(sessionId: number, futureId: number, min: Message, max: Message) {
        this.sendAction('onGetRange', sessionId, futureId, min, max)
    }
    private setRange(...argv) {
        this.meter.min = fromAny(argv[0], Int32Value)
        this.meter.max = fromAny(argv[1], Int32Value)
    }
    private getValue(sessionId: number, futureId: number) {
        const value = toAny(this.meter.value, Int32Value)
        this.onGetValue(sessionId, futureId, value)
    }
    private onGetValue(sessionId: number, futureId: number, value: Message) {
        this.sendAction('onGetValue', sessionId, futureId, value)
    }
    private setValue(...argv) {
        this.meter.value = fromAny(argv[0], Int32Value)
    }
}